package leetcode_700;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *@author 周杨
 *FindDuplicateFileInSystem_609 找重复的文件路径
 *describe:扫描遍历 记录map 很无聊 AC 70%
 *2018年9月19日 上午9:59:13
 */
public class FindDuplicateFileInSystem_609 {
	public List<List<String>> findDuplicate(String[] paths) {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for(String p: paths) {
            String dir[] = p.split(" ");
            findContent(dir, map);
        }
        
        List<List<String>> result = new ArrayList<List<String>>();
        for(String l: map.keySet()) {
            if(map.get(l).size() > 1) //checking for keys which have multiple paths
            result.add(map.get(l));
        }
        
        return result;
    }
    
    public void findContent(String dir[], HashMap<String, List<String>> map) {
        for(int i = 1; i<dir.length; i++) {
            String content = dir[i].substring(dir[i].indexOf('(')+1, dir[i].indexOf(')'));
            String path = dir[i].substring(0, dir[i].indexOf('('));
            List<String> list = map.getOrDefault(content, new ArrayList<String>());
            list.add(dir[0] + "/" + path);
            map.put(content, list);
        }
    }
}
